﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace Eneta.Portal.Literature.Controls
{
    public class LiteratureBottomControl : UserControl
    {
        protected HtmlGenericControl currentUserName;
        protected HtmlAnchor newLiteratureLink;
        protected HtmlAnchor addLiteratureLink;
        protected HtmlGenericControl slideBoxLoggedIn;

        protected Repeater addLinksRepeater;

        public string LiteratureLink { get; set; }
        public string LiteratureLinkText { get; set; }
        public string InterestingLiteratureLinkText { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            addLinksRepeater.ItemDataBound += new RepeaterItemEventHandler(addLinksRepeater_ItemDataBound);
        }

        void addLinksRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var item = e.Item.DataItem as UrlDto;
            if (item == null)
                return;
            var link = e.Item.FindControl("LinksControl") as HtmlAnchor;
            if (link == null)
                return;
            link.HRef = item.Url;
            link.InnerText = item.LinkName;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (SPContext.Current.Web.CurrentUser != null)
            {
                BindSliderBox();
                BindLinks();
            } else {
                this.Visible = false;
            }
        }

        private void BindSliderBox()
        {
            slideBoxLoggedIn.Visible = true;
            this.currentUserName.InnerText = SPContext.Current.Web.CurrentUser.Name;
            this.newLiteratureLink.InnerText = InterestingLiteratureLinkText;
            this.newLiteratureLink.HRef = LiteratureLink;

        }

        private void BindLinks()
        {
            this.addLiteratureLink.InnerText = LiteratureLinkText;
            this.addLiteratureLink.HRef = LiteratureLink;

            var urls = new List<UrlDto>();
            if (LiteratureLink == "/oppimine/ajakirjad/edit.aspx")
            {
                urls.Add(new UrlDto() { Url = "/oppimine/raamatud/edit.aspx", LinkName = Resources.GetResourceString("AddBook") });
            }
            else
            {
                urls.Add(new UrlDto() { Url = "/oppimine/ajakiri/edit.aspx", LinkName = Resources.GetResourceString("AddMagazine") });
            }
            urls.Add(new UrlDto() { Url = "/uudised/teated/addannouncement.aspx", LinkName = Resources.GetResourceString("AddAnnouncement") });
            urls.Add(new UrlDto() { Url = "/addblog.aspx", LinkName = Resources.GetResourceString("AddBlog") });
            urls.Add(new UrlDto() { Url = "/firmad/edit.aspx", LinkName = Resources.GetResourceString("AddCompany") });
            this.addLinksRepeater.DataSource = urls;
            this.addLinksRepeater.DataBind();
        }
    }

    public class UrlDto
    {
        public string Url { get; set; }
        public string LinkName { get; set; }
    }
}
